<?php
//商品控制器
class GoodsController extends BaseController {
	//显示商品
	public function indexAction(){
		include CUR_VIEW_PATH . "goods_list.html";
	}
	//显示添加表单页面
	public function addAction(){
		//获取所有的商品分类
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		//获取所有的品牌
		$brandModel = new BrandModel('brand');
		$brands = $brandModel->getBrands();
		//获取所有的商品类型
		$typeModel = new TypeModel('goods_type');
		$types = $typeModel->getTypes();
		include CUR_VIEW_PATH . "goods_add.html";
	}
	//完成添加动作
	public function insertAction(){
		//1.收集表单数据
		$data['goods_name'] = trim($_POST['goods_name']);
		$data['goods_sn'] = trim($_POST['goods_sn']);
		$data['market_price'] = trim($_POST['market_price']);
		$data['shop_price'] = trim($_POST['shop_price']);
		$data['promote_price'] = trim($_POST['promote_price']);
		$data['promote_start_time'] = strtotime($_POST['promote_start_time']);
		$data['promote_end_time'] = strtotime($_POST['promote_end_time']);
		$data['goods_desc'] = trim($_POST['goods_desc']);
		$data['goods_number'] = trim($_POST['goods_number']);
		$data['is_best'] = isset($_POST['is_best']) ? $_POST['is_best'] : 0;
		$data['is_new'] = isset($_POST['is_new']) ? $_POST['is_new'] : 0;
		$data['is_hot'] = isset($_POST['is_hot']) ? $_POST['is_hot'] : 0;
		$data['is_onsale'] = isset($_POST['is_onsale']) ? $_POST['is_onsale'] : 0;
		$data['cat_id'] = $_POST['cat_id'];
		$data['brand_id'] = $_POST['brand_id'];
		$data['type_id'] = $_POST['type_id'];
		//var_dump $_FILES 
		//var_dump($_FILES['goods_img']);
		if ($_FILES['goods_img']['tmp_name'] != ''){
			//说明有上传
			$this->library('Upload');
			$upload = new Upload();
			if ($filename = $upload->up($_FILES['goods_img'])) {
				//上传成功
				$data['goods_img'] = $filename;
			} else {
				//上传失败
				$this->jump('index.php?p=admin&c=goods&a=add',$upload->error());
			}
		}
		//2.验证和处理
		if ($data['goods_name'] == '') {
			$this->jump('index.php?p=admin&c=goods&a=add','商品名称不能为空');
		}
		//转义
		$this->helper('input');
		$data = deepslashes($data);
		$data = deepspecialchars($data);
		//3.调用模型完成入库，提示并跳转
		$goodsModel = new GoodsModel('goods');
		if ($goods_id = $goodsModel->insert($data)) {
			//goods_attr表的插入
			//在商品插入成功的时候，但是要保证在跳转之前
			//收集表单数据
			//注意，需要判断是否有填写属性，如果没有填写，则无需处理
			if (isset($_POST['attr_id_list'])) { 
				$attr_ids = $_POST['attr_id_list'];
				$attr_values = $_POST['attr_value_list'];
				$attr_prices = $_POST['attr_price_list'];
				//循环插入
				//$goodsAttrModel = new GoodsAttrModel('goods_attr');
				$model = new Model('goods_attr');
				foreach ($attr_ids as $k => $v) {
					$list['attr_id'] = $v;
					$list['attr_value'] = $attr_values[$k] ;
					$list['attr_price'] = $attr_prices[$k];
					$list['goods_id'] = $goods_id ;

					$model->insert($list); //直接插入即可，无需提示，也不用判断
				}
			}
			$this->jump('index.php?p=admin&c=goods&a=index','添加商品成功',2);

		} else {
			$this->jump('index.php?p=admin&c=goods&a=add','添加商品失败');
		}
	}
	//显示修改表单页面
	public function editAction(){
		include CUR_VIEW_PATH . "goods_edit.html";
	}
	//完成更新操作
	public function updateAction(){}
	//删除操作
	public function deleteAction(){}
}